package com.tutu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.reggie.common.CustomException;
import com.tutu.reggie.dto.SetmealDto;
import com.tutu.reggie.entity.Setmeal;
import com.tutu.reggie.entity.SetmealDish;
import com.tutu.reggie.mapper.SetmealMapper;
import com.tutu.reggie.service.SetmealDishService;
import com.tutu.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增套餐
     * @param setmealDto
     */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto){
        //保存套餐的基本信息 操作setmeal表
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐时 同时删除对应菜品的关联信息
     * @param ids
     */
    @Transactional
    public void removeWithDish(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //首先查询删除的套餐是否处于起售状态
        for(Long id:ids){
            queryWrapper.eq(Setmeal::getId,id);
            queryWrapper.eq(Setmeal::getStatus,1);
            Setmeal setmeal = this.getOne(queryWrapper);
            if(!ObjectUtils.isEmpty(setmeal)){
                throw new CustomException(setmeal.getName()+"套餐正在起售,无法删除！");
            }
        }
        //如果可以删除，先删除套餐表setmeal中的数据
        this.removeByIds(ids);
        //删除关系表中的数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 根据id查询套餐信息  同时查询出对应的菜品   setmeal_dish
     * @param id
     */
    public SetmealDto getByIdWithDish(Long id){
        //根据id查询出对应的setmeal信息  setmeal
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //将setmeal的属性拷贝到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询出对应的菜品  setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }


    /**
     * 修改套餐时 同时修改对应的菜品
     * @param setmealDto
     */
    public void updateWithDish(SetmealDto setmealDto){
        //修改套餐表   setmeal
        this.updateById(setmealDto);
        //删除当前套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //添加当前提交的菜品表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> list = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }

    public void updateSetmealStatus(Long[] ids){

    }


    /**
     * 根据setmeal_id查找对应的dish_id
     * @param id
     * @return
     */
    public List<Long> findDishID(Long id){
        return setmealMapper.findDishID(id);
    }
}
